import { http } from '@/utils/http/axios';
import { ContentTypeEnum, RequestEnum } from "@/enums/httpEnum";

export interface BasicResponseModel<T = any> {
  code: number;
  message: string;
  data: T;
}

export interface BasicPageParams {
  pageNumber: number;
  pageSize: number;
}

/**
 * @description: 获取用户信息
 */
export function getUserInfo() {
  return http.request({
    url: `/user`,
    method: 'get',
  });
}

/**
 * @description: 用户登录
 */
export function login(params) {
  return http.request<BasicResponseModel>(
    {
      url: '/login',
      method: RequestEnum.POST,
      params,
    },
    {
      isReturnNativeResponse: true,
    }
  );
}

/** 分页获取用户信息 */
export function getUserPage(
  info: BasicPageParams,
  params: {
    username?: string,
    status?: number,
    startTime?: string,
    endTime?: string
  }
) {
  return http.request({
    url: `/user/page/${info.pageNumber}/${info.pageSize}`,
    method: RequestEnum.GET,
    params
  })
}

/** 更新用户信息 */
export function updateInfo(data) {
  return http.request({
    url: `/user`,
    method: RequestEnum.PUT,
    data
  })
}

/** 用户注册 */
export function register(data) {
  return http.request({
    url: '/register',
    method: RequestEnum.POST,
    data
  }, {
    isTransformResponse: false
  })
}

/** 修改用户的状态 */
export function updateStatus(id: number) {
  return http.request({
    url: `/user/status/${id}`,
    method: RequestEnum.PUT
  }, {
    isShowSuccessMessage: true
  })
}

/** 修改用户角色 */
export function updateRole(userId: number, roleId: number) {
  return http.request({
    url: `/user/role/${userId}/${roleId}`,
    method: RequestEnum.PUT
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * @description: 用户修改密码
 */
export function changePassword(data) {
  return http.request(
    {
      url: `/user/password`,
      method: RequestEnum.POST,
      data
    }
  );
}

/**
 * 重置用户密码
 * @param userId
 * @param password
 */
export function resetUserPwd(userId: number, password: string) {
  return http.request(
    {
      url: `/user/password/${userId}`,
      method: RequestEnum.PUT,
      data: password,
      headers: {
        'Content-Type': ContentTypeEnum.TEXT
      }
    }, {
      isShowSuccessMessage: true
    }
  )
}


/**
 * @description: 用户登出
 */
export function logout() {
  return http.request({
    url: '/logout',
    method: RequestEnum.POST
  });
}

/** 请求绑定/解绑邮箱 */

/**
 * @description: 请求绑定邮箱
 */
export function reqBindEmail(email: string) {
  return http.request({
    url: `/user/bind/email/${email}`,
    method: RequestEnum.GET
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * @description: 请求解绑邮箱
 */
export function reqUnbindEmail() {
  return http.request({
    url: `/user/unbind/email`,
    method: RequestEnum.GET
  }, {
    isShowSuccessMessage: true
  })
}

/** 处理绑定/解绑邮箱 */

/**
 * @description 处理绑定邮箱
 */
export const bindEmail = (email, code) => {
  return http.request({
    url: `/common/bind/email/${email}/${code}`,
    method: RequestEnum.GET
  }, {
    isTransformResponse: false
  })
}

/**
 * @description 处理解绑邮箱
 */
export const unbindEmail = (email, code) => {
  return http.request({
    url: `/common/unbind/email/${email}/${code}`,
    method: RequestEnum.GET
  }, {
    isTransformResponse: false
  })
}

/** 重置密码 */

/**
 * @description 验证用户名(重置密码)
 */
export const checkUsername = (username) => {
  return http.request({
    url: `/common/reset/check/user/${username}`,
    method: RequestEnum.GET
  }, {
    isTransformResponse: false
  })
}

/**
 * @description 重置密码
 */
export const resetPwdByEmail = ({ email, code, password }) => {
  return http.request({
    url: `/common/reset/password/${email}/${code}`,
    method: RequestEnum.POST,
    data: password,
    headers: {
      'Content-Type': ContentTypeEnum.TEXT
    }
  }, {
    isTransformResponse: false
  })
}
